home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / Mic-1 v1.0 / Project and Source / Source / mic_main.cpp < prev    next >
Text File  |  1996-05-11  |  4KB  |  159 lines

  1. /*
  2.     "main.cpp"
  3.     
  4.     Here is the main code. Maybe later (if I have the time) I will turn this
  5.     into a real Mic-1 simulator with a mac interface.
  6. */
  7.  
  8. #include <iostream.h>
  9. #include <fstream.h>
  10.  
  11. #include "mic_main.h"
  12. #include "mic_global_functions.h"
  13. #include "mic_dump.h"
  14. #include "mic_clock.h"
  15. #include "mic_memory.h"
  16. #include "mic_data_path.h"
  17. #include "mic_control.h"
  18.  
  19. // cycleBreakPoint
  20.     const short kRunConstant = 0;
  21.     const short kRunInterrupted = 1;
  22.  
  23. DataRec Data;
  24. OptionsRec options;
  25.  
  26. // Local prototypes
  27. void InitGlobalDataRec (void);
  28. void mainMicScreen (void);
  29. void doAbout (void);
  30. void doGo (Mic_1_Class& Mic, short cycleBreakPoint);
  31. void doOptions (Mic_1_Class& Mic);
  32.  
  33. void main (void)
  34. {
  35.     char        c = 'r';
  36.     short        error = 0;
  37.     long        tickStart, ticks;
  38.         
  39.     while ((c == 'r') || (c == 'R'))    // if char is 'r' or 'R', they want to reset it.
  40.     {
  41.         Mic_1_Class Mic;
  42.         // we have a separate init procs (instead of a constructor) to add error checking
  43.         error = InitMicMemory(Mic);
  44.         error = InitMicControl(Mic);
  45.         
  46.         InitGlobalDataRec();
  47.         
  48.         cout << "Welcome to a cool Mic-1 simulator" << endl;
  49.         cout << "  by John DeWeese in April 1996" << endl;
  50.         mainMicScreen();
  51.         
  52.         if (error)
  53.             Death(error);
  54.         
  55.         while (!Data.Done)
  56.         {
  57.             cout << ">> ";
  58.             cin >> c;
  59.             switch (c)
  60.             {
  61.                 case '?': doAbout(); break;                                                                                            // ? = about box
  62.                 case 'd': case 'D': doDump(Mic); mainMicScreen(); break;                                // d = dump something
  63.                 case 'o': case 'O': doOptions(Mic); mainMicScreen(); break;                            // o = options
  64.                 case 'c': case 'C': doGo(Mic, kRunInterrupted); mainMicScreen(); break;    // c = cycle once
  65.                 case 'g': case 'G': doGo(Mic, kRunConstant); mainMicScreen(); break;        // g = GO!
  66.                 case 'r': case 'R':                                                                                                            // r = reset
  67.                 case 'x': case 'X': case 'q': case 'Q':    Data.Done = true; break;                // x = exit
  68.                 default: cout << "huh?" << endl; break;
  69.             }
  70.         }
  71.         
  72.         if ((c == 'q') || (c == 'Q'))
  73.         {
  74.             cout << endl << "Dumping to the final file...";
  75.             Dump(Mic, kFromEverything, kToFinalFile);
  76.             cout << " done." << endl;
  77.             cout << endl;
  78.         }
  79.     }
  80.     cout << "<done>";
  81. }
  82.  
  83. void InitGlobalDataRec (void)
  84. {
  85.     Data.Done = false;
  86.     Data.doneExecuting = false;
  87.     Data.Cycle = 0;
  88.     Data.CPS = 0.0;
  89. }
  90.  
  91. void mainMicScreen (void)
  92. {    
  93.     cout << "    ___________" << endl;
  94.     cout << "___/ MAIN MENU \\____________________________" << endl;
  95.     cout << "| ? - About   |  d - Dump to a stream       |" << endl;
  96.     cout << "| o - Options |  c - Cycle once             |" << endl;
  97.     cout << "| r - Reset   |  g - GO! (Continuous cycle) |" << endl;
  98.     cout << "|-------------+-----------------------------|" << endl;
  99.     cout << "| x - Exit    |  q - Quit (dump&exit)       |" << endl;
  100.     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  101.     cout << "       cycles = " << Data.Cycle << endl;
  102.     cout << "cycles/second = "; if (Data.CPS > 0) cout << Data.CPS; else cout << "-";
  103.     cout << endl << endl;
  104. }
  105.  
  106. void doAbout (void)
  107. {
  108.     InitCursor();
  109.     Alert(129, nil);
  110. }
  111.  
  112. void doGo (Mic_1_Class& Mic, short cycleBreakPoint)
  113. {
  114.     long startCycle = Data.Cycle, diffCycle = 1;
  115.     long ticks;
  116.     Boolean userBreak = false;
  117.     
  118.     ticks = TickCount();
  119.     do
  120.     {
  121.         CYCLE(Mic);                    // do one full clock cycle
  122.         Data.Cycle++;
  123.         diffCycle++;
  124.         
  125.         if (CheckForEscapeKey())
  126.             userBreak = true;
  127.         diffCycle = 1;
  128.     }
  129.     while ((cycleBreakPoint == kRunConstant) && (!Data.doneExecuting) && (!userBreak));
  130.     if ((ticks = TickCount() - ticks) > 0)
  131.         Data.CPS = 60*(Data.Cycle - startCycle)/(ticks);
  132.     else Data.CPS = 0.0;
  133. }
  134.  
  135. void doOptions (Mic_1_Class& Mic)
  136. {
  137.     Boolean done = false;
  138.     char c;
  139.     
  140.     cout << endl;
  141.     cout << "__OPTIONS________________________________" << endl;
  142.     cout << "Set the options for running the Mic-1" << endl;
  143.     cout << " <none so far>" << endl;
  144.     cout << " x - escape back to the previos menu" << endl;
  145.     cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
  146.     
  147.     while (!done)
  148.     {
  149.         cout << "options >> ";
  150.         cin >> c;
  151.         switch (c)
  152.         {
  153.             case 'x': case 'X': done = true; break;
  154.             default: cout << "huh?" << endl; break;
  155.         }
  156.     }
  157. }
  158.  
  159.